import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common'
import { Response } from 'express'

// 异常拦截器
@Catch(HttpException)
export class CatchFilter implements ExceptionFilter {
    catch(exception: HttpException, host: ArgumentsHost) {
        const ctx = host.switchToHttp()
        const response = ctx.getResponse<Response>()
        const status = exception.getStatus()
        if (status === 401) {
            response.status(status).json({
                data: null,
                code: '4001',
                tips: `您无权限，不该看的就别看啦：～`
            })
        } else {
            response.status(status).json({
                data: null,
                code: '5000',
                tips: `哦豁，接口请求异常了，真奇怪：～`
            })
        }
    }
}